package medium;

import java.util.List;

public class Lc1239 {
    class Solution {

        private int ret = 0;

        List<String> array;

        public int maxLength(List<String> arr) {
            array = arr;
            dfs("", 0);
            return ret;
        }

        private void dfs(String cur, int index) {
            if (index >= array.size()) {
                ret = Math.max(cur.length(), ret);
                return;
            }

            if (canAddToCur(cur, array.get(index))) {
                dfs(cur + array.get(index), index + 1);
            }
            dfs(cur, index + 1);
        }

        private boolean canAddToCur(String cur, String add) {
            String addExt = "";
            for (char c : add.toCharArray()) {
                if (addExt.contains(String.valueOf(c))) {
                    return false;
                }
                addExt += String.valueOf(c);
                if (cur.contains(String.valueOf(c))) {
                    return false;
                }
            }
            return true;
        }
    }
}
